package com.example.demo.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.po.Work;
import com.example.demo.service.WorkService;

@RestController
@RequestMapping("/api/work")
public class WorkApi {
	@Autowired
	private WorkService WorkService;

	@RequestMapping("/all")
	public List<Work> all() {
		return WorkService.selectAll();
	}

	@RequestMapping("/one")
	public Work one(Integer id) {

		return WorkService.selectById(id);
	}

	@RequestMapping("/one1/{id}")
	public Work one1(@PathVariable("id") Integer id) {
		return WorkService.selectById(id);
	}

	@RequestMapping("/add")
	public String add(Work Work) {
		WorkService.insert(Work);
		return "添加成功！";

	}

	@RequestMapping("/add1")
	public String add(@RequestBody JSONObject json) {
		Work Work = null;
		Work = JSON.toJavaObject(json, Work.class);
		System.out.println("添加成功");
		WorkService.insert(Work);
		return "添加成功";

	}

	@RequestMapping("/update1")
	public String update(@RequestBody JSONObject json) {
		System.out.println(11111111);
		Work Work = null;

		Work = JSON.toJavaObject(json, Work.class);
		WorkService.update(Work);
		return "修改成功";
	}

	@RequestMapping("/update")
	public String update(Work Work) {
		WorkService.update(Work);
		return "修改成功";
	}

	@RequestMapping("/delete")
	public String delete(Integer id) {
		WorkService.delete(id);
		return "删除成功！";
	}

	@RequestMapping("/delete1/{id}")
	public String delete1(@PathVariable("id") Integer id) {
		WorkService.delete(id);
		return "删除成功";
	}

}
